package com.huadi.firematerialident.controller;


import com.huadi.firematerialident.po.FireInfo;
import com.huadi.firematerialident.service.FireInfoServiceImpl;
import com.huadi.firematerialident.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/fireinfo")
public class FireInfoController {

    @Autowired
    FireInfoServiceImpl fireInfoService;
    @Autowired
    UserServiceImpl userService;

    @GetMapping("/FireInfoAdd")
    public String addFireInfo(@RequestParam("FireType") String ftype,
                              @RequestParam("FireLevel") String flevel,
                              @RequestParam("FireTime") String ftime,
                              @RequestParam("FireAdd") String fadd,
                              @RequestParam("MoreInfo") String moreinfo, Model model) throws ParseException {


        FireInfo fireInfo = new FireInfo();
        LocalDate firetime = LocalDate.parse(ftime);  //LocalDateTime
        fireInfo.setFireInfoID();
        fireInfo.setFireType(ftype);
        fireInfo.setFireLevel(flevel);
        fireInfo.setFireTime(firetime);
        fireInfo.setFireAdd(fadd);
        fireInfo.setMoreInfo(moreinfo);

        if(fireInfoService.addFireInfo(fireInfo)==0){
            model.addAttribute("msg","添加失败");
        }
        else{
            model.addAttribute("msg","添加成功");
        }
        return "/fire/firecommit";
    }


    @GetMapping("/FireInfoSel")
    @ResponseBody
    public String selFireInfo(@PathParam("FireInfo") String FireInfoID, Model model){


        List<Map<String,Object>> list = fireInfoService.selectFireInfoByID(FireInfoID);
        if(list.isEmpty()){
            model.addAttribute("msg","没有查询到数据。");

        }
        else{
            model.addAttribute("msg","查询成功！");
        }
        return userService.ListToJSON(list.size(),list);
    }

    @GetMapping("/getAllFireInfo")
    @ResponseBody
    public String getAllFireInfo(Model model){


        List<Map<String,Object>> list = fireInfoService.getAllFireInfo();
        if(list.isEmpty()){
            model.addAttribute("msg","没有找到数据");
            return "/fire/fireinfo";
        }
        else{
            return userService.ListToJSON(list.size(),list);
        }
    }

}
